<template>
  <div class="app-container">
    <!-- 添加或修改主子演示对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" v-if="open" append-to-body>
      <el-form ref="materRef" :model="form" :rules="rules" label-width="110px" :disabled="disabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="律师所名称" prop="lawFirmName">
              <el-input v-model="form.lawFirmName" placeholder="请输入律师所名称" maxlength="30" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址" maxlength="255" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="logo图片id" prop="fileId">
              <image-upload v-model="form.fileId"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="state">
              <el-select v-model="form.state" placeholder="请选择状态">
                <el-option
                  v-for="dict in flow_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布时间" prop="publishTime">
        <el-date-picker
                clearable size="small"
                v-model="form.publishTime"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="选择发布时间">
        </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">子信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddTestSub">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteTestSub">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="testSubList" :row-class-name="rowTestSubIndex" @selection-change="handleTestSubSelectionChange" ref="testSub">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="律师名称" prop="lawyerName" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.lawyerName" placeholder="请输入律师名称" />
            </template>
          </el-table-column>
          <el-table-column label="联系电话" prop="phone" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.phone" placeholder="请输入联系电话" />
            </template>
          </el-table-column>
          <el-table-column label="简介" prop="briefIntroduction" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.briefIntroduction" placeholder="请输入简介" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" v-if="!disabled" @click="submitForm">确 定</el-button>
          <el-button @click="cancel" v-if="!disabled">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Dialog">
import { getMater, addMater, updateMater } from "@/api/test/mater";

const { proxy } = getCurrentInstance();
const { flow_status } = proxy.useDict('flow_status');

const testSubList = ref([]);
const open = ref(false);
const disabled = ref(false);
const checkedTestSub = ref([]);
const title = ref("");

const data = reactive({
  form: {},
  rules: {
  }
});

const emit = defineEmits(["refresh"]);
const { form, rules } = toRefs(data);

/** 打开主子演示弹框 */
async function show(id, _disabled) {
  reset();
  disabled.value = _disabled
  if (id) {
    await getMater(id).then(response => {
      form.value = response.data;
      testSubList.value = response.data.testSubList;
    });
  }
  open.value = true
  if (disabled.value) {
    title.value = "详情"
  } else if (id) {
    title.value = "修改"
  } else {
    title.value = "新增"
  }
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    tenantId: null,
    lawFirmName: null,
    address: null,
    fileId: null,
    delFlag: null,
    state: null,
    publishTime: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  testSubList.value = [];
  proxy.resetForm("materRef");
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["materRef"].validate(valid => {
    if (valid) {
      form.value.testSubList = testSubList.value;
      if (form.value.id != null) {
        updateMater(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          emit('refresh');
        });
      } else {
        addMater(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          emit('refresh');
        });
      }
    }
  });
}

/** 子序号 */
function rowTestSubIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 子添加按钮操作 */
function handleAddTestSub() {
  let obj = {};
  obj.lawyerName = "";
  obj.phone = "";
  obj.briefIntroduction = "";
  testSubList.value.push(obj);
}

/** 子删除按钮操作 */
function handleDeleteTestSub() {
  if (checkedTestSub.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的子数据");
  } else {
    const testSubs = testSubList.value;
    const checkedTestSubs = checkedTestSub.value;
    testSubList.value = testSubs.filter(function(item) {
      return checkedTestSubs.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleTestSubSelectionChange(selection) {
  checkedTestSub.value = selection.map(item => item.index)
}

defineExpose({
  show,
});
</script>
